﻿Imports System.IO

<ContentType()>
Public Class FileContent
    Inherits BaseContent

    <ContentProperty()>
    Public Property name As ContentPropertyValue(Of String) = ""
    <ContentProperty()>
    Public Property location As ContentPropertyValue(Of String) = ""
    <ContentProperty()>
    Public Property web_position As ContentPropertyValue(Of String) = ""


    Public Class FileContentGenerationResult
        Public Property valid_input As Boolean = False
        Public Property success As Boolean = False
        Public Property file_name_preserved As Boolean = False

        Public Property error_message As String
    End Class
    Public Function generatefrom(local_file As FileInfo) As FileContentGenerationResult
        Dim result As New FileContentGenerationResult
        If local_file Is Nothing Then
            result.error_message = "nothing argument"
            Return result
        End If

        If Not local_file.Exists Then
            result.error_message = "the given file path does not exsist"
            Return result
        End If

        result.valid_input = True
        Me.name = local_file.Name


        Dim root As DirectoryInfo = getGlobalObject(Of MysteryConfiguration)().files_root_folder
        If root Is Nothing OrElse Not root.Exists Then
            result.error_message = "myster not set"
            Return result
        End If

        Dim y As String = Date.Now.Year.ToString
        Dim m As String = Date.Now.Month.ToString.PadLeft(2, "0"c)

        Dim new_path As String = Path.Combine(root.FullName, y)
        If Not Directory.Exists(new_path) Then
            root.CreateSubdirectory(y)
        End If

        root = New DirectoryInfo(new_path)

        new_path = Path.Combine(new_path, m)
        If Not Directory.Exists(new_path) Then
            root.CreateSubdirectory(m)
        End If

        root = New DirectoryInfo(new_path)

        Dim guid_name As String = guid.NewGuid().ToString & local_file.Extension


        If File.Exists(Path.Combine(new_path, name)) Then
            result.file_name_preserved = False
        Else
            result.file_name_preserved = True
            guid_name = name
        End If

        new_path = Path.Combine(new_path, guid_name)


        File.Copy(local_file.FullName, new_path)


        Me.location = Path.Combine(y, m, guid_name)

        result.success = True

        Return result

    End Function

End Class
